﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Silvermoon.Windows;
using Silvermoon.Controls.Classes;
using Silvermoon.Controls.Base;
using Silvermoon.UI;
using Silvermoon.Interfaces;
using Silvermoon.Shapes;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Abstract control for scroll bars.
    /// </summary>
    public abstract class ScrollBar : Control, IColor
    {
        #region Fields

        protected int position;
        protected int minimum;
        protected int maximum = 100;
        protected int largeChange = 1;
        private bool showBackground = true;

        #endregion
        #region ctor/dtor

        public ScrollBar()
            : base()
        {
            ControlFlags |= ControlFlags.CanPan;
        }


        #endregion
        #region Properties

        public override bool CanFocus
        {
            get { return false; }
        }

        protected abstract Shape Gripper
        {
            get;
        }


        /// <summary>
        /// Gets or sets the color of the gripper.
        /// </summary>
        public override Color Color
        {
            get { return Gripper.Color; }
            set
            {
                if (Gripper.Color != value)
                {
                    Gripper.Color = value;
                    Invalidate(InvalidationFlag.Color);
                }
            }
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        public int Value
        {
            get { return position; }
            set { if (position != value) { position = value; OnValueChanged(); Invalidate(InvalidationFlag.Appearance | InvalidationFlag.Layout); } }
        }

        /// <summary>
        /// Gets or sets the minimum value.
        /// </summary>
        public int Minimum
        {
            get { return minimum; }
            set { if (value < 0) value = 0; if (minimum != value) { minimum = value; Invalidate(InvalidationFlag.Appearance | InvalidationFlag.Layout); } }
        }

        /// <summary>
        /// Gets or sets the maximum value.
        /// </summary>
        public int Maximum
        {
            get { return maximum; }
            set { if (value < 0) value = 0; if (maximum != value) { maximum = value; Invalidate(InvalidationFlag.Appearance | InvalidationFlag.Layout); } }
        }

        public int LargeChange
        {
            get { return largeChange; }
            set { if (largeChange != value) { largeChange = value; Invalidate(InvalidationFlag.Appearance | InvalidationFlag.Layout); } }
        }

        public bool ShowBackground
        {
            get { return showBackground; }
            set
            {
                if (showBackground != value)
                {
                    showBackground = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        #endregion
        #region Methods

        protected virtual void OnValueChanged()
        {
            if (ValueChanged != null) ValueChanged(this, EventArgs.Empty);
        }
        #endregion

        public event EventHandler ValueChanged;

        #region IScrollEventBehavior Members


        protected internal override void OnBeginPan(PanEventArgs e)
        {
            base.OnBeginPan(e);
            e.Handled = true;
        }

        #endregion

        public static readonly PropertyAccessor<ScrollBar, int> ValueProperty = PropertyAccessor.Register<ScrollBar, int>("Value", e => ((ScrollBar)e).Value, (e, v) => ((ScrollBar)e).Value = v);

    }
}
